package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.ExamOutline;
import com.yunji.news.mapper.ExamOutlineMapper;
import com.yunji.news.service.ExamOutlineService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 校测考纲服务实现类
 *
 * @author yunji
 * @since 2024-11-26
 */
@Service
public class ExamOutlineServiceImpl extends ServiceImpl<ExamOutlineMapper, ExamOutline> 
        implements ExamOutlineService {

    @Override
    public IPage<ExamOutline> getOutlinePage(Integer page, Integer size, 
                                            Long schoolId, Long majorId, 
                                            Integer examYear, String examType, String keyword) {
        Page<ExamOutline> pageParam = new Page<>(page, size);
        return baseMapper.selectOutlinePage(pageParam, schoolId, majorId, examYear, examType, keyword);
    }

    @Override
    public boolean createOutline(ExamOutline outline) {
        outline.setStatus(1);
        outline.setDownloadCount(0);
        return save(outline);
    }

    @Override
    public boolean updateOutline(ExamOutline outline) {
        return updateById(outline);
    }

    @Override
    public boolean publishOutline(Long id) {
        LambdaUpdateWrapper<ExamOutline> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ExamOutline::getId, id)
               .set(ExamOutline::getStatus, 2)
               .set(ExamOutline::getPublishTime, LocalDateTime.now());
        return update(wrapper);
    }

    @Override
    public List<ExamOutline> getPublishedOutlines(Long schoolId, Long majorId, Integer examYear) {
        LambdaQueryWrapper<ExamOutline> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExamOutline::getStatus, 2)
               .eq(schoolId != null, ExamOutline::getSchoolId, schoolId)
               .eq(majorId != null, ExamOutline::getMajorId, majorId)
               .eq(examYear != null, ExamOutline::getExamYear, examYear)
               .orderByDesc(ExamOutline::getPublishTime)
               .orderByAsc(ExamOutline::getSortOrder);
        return list(wrapper);
    }

    @Override
    public boolean recordDownload(Long id) {
        LambdaQueryWrapper<ExamOutline> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamOutline::getId, id);
        ExamOutline outline = getOne(queryWrapper);
        
        if (outline != null) {
            LambdaUpdateWrapper<ExamOutline> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ExamOutline::getId, id)
                         .set(ExamOutline::getDownloadCount, outline.getDownloadCount() + 1);
            return update(updateWrapper);
        }
        return false;
    }

    @Override
    public List<ExamOutline> getPopularOutlines(Integer limit) {
        LambdaQueryWrapper<ExamOutline> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExamOutline::getStatus, 2)
               .orderByDesc(ExamOutline::getDownloadCount)
               .orderByDesc(ExamOutline::getPublishTime)
               .last("LIMIT " + limit);
        return list(wrapper);
    }
}